<template>
  <!-- 用户管理 -->
  <div class="dashboard-container">
    <SearchTable :show-pagination="false">
      <template slot="header">
        <el-row style="width: 100%">
          <el-input
            style="width: 300px"
            @input="searchKeyWord"
            clearable
            placeholder="请输入用户名搜索关键词"
            v-model="keyWord"
          ></el-input>
        </el-row>
        <el-button type="primary" @click="addStore">注册用户</el-button>
      </template>
      <template>
        <el-table :data="tableData" height="675" border style="width: 100%">
          <el-table-column type="index" label="序号" align="center" width="60"></el-table-column>
          <el-table-column prop="username" label="用户名" align="center"></el-table-column>
          <el-table-column prop="nickname" label="姓名" align="center"></el-table-column>
          <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
          <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template v-slot="{ row }">
              <div class="link-list">
                <el-link :underline="false" type="primary" @click="edit(row)">编辑</el-link>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </SearchTable>

    <!-- 弹框 -->
    <AddDialog
      :storeData="storeForm"
      :title="title"
      v-if="addVisible"
      :visible.sync="addVisible"
      @confirm="addStoreConfirm"
    ></AddDialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import SearchTable from "@/components/TableTemplate/SearchTable";
import { getAllUserInfo, updateUserInfo } from "@/api/user";
import AddDialog from "./components/AddDialog";
export default {
  name: "Dashboard",
  components: {
    SearchTable,
    AddDialog,
  },
  data() {
    return {
      tableData: [],
      originTableData: [],
      addVisible: false,
      title: "注册用户",
      storeForm: {},
      imgDialogVisible: false,
      imgDialogData: [],
      keyWord: "",
      timer: null,
    };
  },
  mounted() {
    this.getAllUserInfo();
  },
  computed: {
    ...mapGetters(["name", "token"]),
  },
  methods: {
    showImgDialog(storeImg) {
      this.imgDialogVisible = true;
      this.imgDialogData = storeImg;
    },
    async getAllUserInfo() {
      const { data: res } = await getAllUserInfo();
      this.tableData = res || [];
      this.originTableData = res || [];
    },
    searchKeyWord(keyWord) {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(() => {
        //这里setTimeout用来防抖，也就是500ms内一直在输入，就会清除上次的定时器，就不会一直搜索，等输入完了之后才真正执行一次搜索，优化

        //先复原
        this.tableData = this.originTableData;
        //再过滤
        let arr = [];
        this.tableData.map((item) => {
          if (item.username.indexOf(keyWord) !== -1) {
            arr.push(item);
          }
        });
        this.tableData = arr;
      }, 500);
    },
    addStore() {
      this.title = "注册用户";
      this.addVisible = true;
      this.storeForm = {};
    },
    edit(row) {
      this.title = "编辑用户";
      this.addVisible = true;
      this.storeForm = row;
    },
    addStoreConfirm() {
      //刷新列表
      this.getAllUserInfo();
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-container {
  width: 100%;
  height: 100%;
}
.link-list {
  .el-link {
    margin: 0 2px;
  }
}
.img-box {
  margin-left: 65px;
  width: 80px;
  height: 120px;
  display: block;
}
</style>
